import requests
import re
import down_bilibili

# pip install ffmpeg-python

if __name__ == '__main__':

    author_id = '1140672573'  # 小王Albert
    author_id = '20819786'  # 秋荻__
    author_id = '1117551831'  # 江湖弃子
    author_id = '33785777'  # 巧绛
    # author_id = '510856133'  # 吟游诗人基德
    # author_id = '397490386'  # 肝帝董佳宁

    pn = 1
    title_bvid_s = []
    while True:
        headers = {
            'authority': 'api.bilibili.com',
            'method': 'GET',
            'path': '/x/space/arc/search?mid=' + author_id + '&ps=50&tid=0&pn=' + str(pn) + '&keyword=&order=pubdate&jsonp=jsonp',
            'scheme': 'https',
            'accept': 'application/json, text/plain, */*',
            'accept-encoding': 'gzip, deflate, br',
            'accept-language': 'zh-CN,zh;q=0.9',
            'cookie': "buvid3=C6860B41-B488-5A44-CC44-CF731BA66EF906654infoc; rpdid=|(k|mYuuYlu)0J'uYk)Jk~)um; i-wanna-go-back=-1; fingerprint=85d2b649a491e650a811d9a4a181c65e; buvid_fp=C6860B41-B488-5A44-CC44-CF731BA66EF906654infoc; buvid_fp_plain=undefined; CURRENT_BLACKGAP=0; LIVE_BUVID=AUTO2916501161447458; blackside_state=0; nostalgia_conf=-1; CURRENT_FNVAL=4048; DedeUserID=1274324748; DedeUserID__ckMd5=45fbc0aaba890df5; bp_video_offset_1274324748=698367145655926800; SESSDATA=ad17446c%2C1677251208%2C73d61%2A81; bili_jct=f04f98985f029a6d2ecfa1797f73e674; sid=62nkq25r; CURRENT_QUALITY=16; innersign=0; b_ut=5",
            'origin': 'https://space.bilibili.com',
            'referer': 'https://space.bilibili.com/' + author_id + '/video',
            'sec-fetch-dest': 'empty',
            'sec-fetch-mode': 'cors',
            'sec-fetch-site': 'same-site',
            'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36"
        }

        url = 'https://api.bilibili.com/x/space/arc/search?mid=' + author_id + '&ps=50&tid=0&pn=' + str(pn) + '&keyword=&order=pubdate&jsonp=jsonp'

        doc = requests.get(url, headers=headers).text
        pat = '"title":"(.*?)".*?"bvid":"(.*?)"'
        # print(doc)

        r = re.compile(pat, re.S).findall(doc)
        for i in r:
            title_bvid_s.append(i)
            print(len(title_bvid_s), i)
        pn += 1
        if len(r) == 0:
            break

    range_start = input('从第几集开始？')
    range_end = input('到第几集结束？')
    if int(range_start) <= int(range_end):
        for i in range(int(range_start), int(range_end)+1):
            video_url = 'https://www.bilibili.com/video/' + title_bvid_s[i-1][1] + '/?spm_id_from=333.788&vd_source=a248f44e3c87683800d8c8bac20ed6b4'
            down_bilibili.GetBiliVideo(video_url, 0)